package com.saicmotor.pay.db;

import com.saicmotor.pay.db.entity.Pay;
import com.saicmotor.pay.db.gen.PayDao;
import com.zone.sdk.common.http.utils.BeanUtils;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @Author: pkqlqx
 * @CreateDate: 2021/12/7 16:58
 * @Description: 初始化、存放及获取DaoUtils
 */
public class PayDaoUtilsStore {
    private volatile static PayDaoUtilsStore instance = new PayDaoUtilsStore();
    private PayDaoUtils<Pay> payPayDaoUtils;

    public static PayDaoUtilsStore getInstance() {
        return instance;
    }

    private PayDaoUtilsStore() {
        PayDaoManager mManager = PayDaoManager.getInstance();
        PayDao PayDao = mManager.getDaoSession().getPayDao();
        payPayDaoUtils = new PayDaoUtils<>(Pay.class, PayDao);
    }

    public PayDaoUtils<Pay> getPayAppDaoUtils() {
        return payPayDaoUtils;
    }

    public Pay getPayByCode(String appCode, String userId){
        if (StringUtils.isEmpty(appCode) || StringUtils.isEmpty(userId)){
            return null;
        }
        List<Pay> PayList = payPayDaoUtils.queryByQueryBuilder(PayDao.Properties.AppCode.eq(appCode), PayDao.Properties.UserId.eq(userId));
        return ObjectUtils.isNotEmpty(PayList)?PayList.get(0):null;
    }

    public boolean deleteByCode(String appCode, String userId){
        Pay Pay = getPayByCode(appCode, userId);
        if (Pay != null){
            return payPayDaoUtils.delete(Pay);
        }
        return false;
    }

    public boolean update(Pay pay){
        Pay old = getPayByCode(pay.getAppCode(), pay.getUserId());
        if (old != null){
            try {
                BeanUtils.copyProperties(pay, old);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return payPayDaoUtils.update(old);
        }
        return false;
    }

}